<?php

namespace app\wx\validate;

use think\Validate;

class UserValidate extends Validate
{
    protected $rule =   [
        'id' => [
            'require'
        ],
        'userid' => [
            'require',
            'max' => 64,
            'regex' => '/^[1-9a-zA-Z_\-@\.]{1,}$/',
            'unique' => 'user'
        ],
        'name' => [
            'require',
            'max' => 64,
        ],
        'alias' => [
            'max' => 32
        ],
        'mobile_area_code' => [
            'regex' => '/^\+[0-9]{2,}/'
        ],
        'mobile' => [
            'unique' => 'user'
        ],
        'department' => [
            'require',
            'checkListMax100' => '成员所属部门id列表,不得超过100个'
        ],
        'order' => [
            'checkListMax100' => '成员所属部门排序值列表,不得超过100个'
        ],
        'position' => [
            'max' => 128,
        ],
        'gender' => [
            'between' => '0,2'
        ],
        'email' => [
            'email',
            'max' => 64,
            'min' => 6,
            'unique' => 'user'
        ],
        'telephone' => [
            'max' => 32,
            'regex' => '/^[0][1-9]{2,3}-[0-9]{5,10}$/'
        ],
        'is_leader_in_dept' => [
            'checkListMax100' => '成员所属部门是否为上级列表,不得超过100个'
        ],
        'enable' => [
            'between' => '0,1'
        ],
        'main_department' => [
            'integer'
        ]
    ];
    
    protected $message  =   [
        'id.require'              => 'ID不能为空',
        'userid.require'          => 'UserID必须',
        'userid.max'              => 'UserID最多不能超过64个字符',
        'userid.regex'            => 'UserID只允许数字、字母和‘_-@.’',
        'userid.unique'           => '请更换UserID，该UserID已被注册',
        'name.require'            => '名称必须',
        'name.max'                => '名称长度最大64个字符',
        'alias.max'               => '别名长度最大支持32个字符',
        'mobile_area_code.regex'  => '手机区号格式不正确',
        'mobile.unique'           => '请更换手机号，该手机号已被注册',
        'department.require'      => '成员所属部门id列表必须',
        'position.max'            => '职务信息最大长度128',
        'gender.between'          => '性别字段应用数字0-2表示',
        'email.email'             => '请输入正确的邮箱格式',
        'email.max'               => '输入长度不正确，6~64位字符串',
        'email.min'               => '输入长度不正确，6~64位字符串',
        'email.unique'            => '请更换邮箱，该邮箱已被注册',
        'telephone.max'           => '座机格式不正确，最大长度32位',
        'telephone.regex'         => '座机格式不正确，应该由数字和‘-’组成',
        'enable.between'          => '状态字段应用数字0-1表示',
        'main_department.integer' => '主部门ID必须为整型'
    ];

    protected $scene = [
        'add' => ['userid','name','alias','mobile_area_code','mobile','department','order','position','gender','email','telephone','is_leader_in_dept','enable','main_department'],
        'edit' => ['id','userid','name','alias','mobile_area_code','mobile','department','order','position','gender','email','telephone','is_leader_in_dept','enable','main_department']
    ];

    //数据类型和长度验证
    protected function checkListMax100($value,$rule,$data=[]){
        if(!is_array($value)) return '数据类型不正确，请上传数组类型';
        if(count($value) > 100) return $rule;
        return true;
    }

}


